<?php

/**
 * This is the model class for table "{{ord_discount}}".
 *
 * The followings are the available columns in table '{{ord_discount}}':
 * @property integer $zod_id
 * @property string $zo_id
 * @property integer $zp_id
 * @property integer $zti_id
 * @property double $zod_price
 * @property integer $zod_type
 */
class OrdDiscount extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return OrdDiscount the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{ord_discount}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zo_id', 'required'),
			array('zp_id, zti_id, zop_id, zod_type', 'numerical', 'integerOnly'=>true),
			array('zod_price', 'numerical'),
			array('zo_id', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zod_id, zo_id, zp_id, zti_id, zod_price, zod_type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zod_id' => 'Zod',
			'zo_id' => 'Zo',
			'zp_id' => 'Zp',
			'zti_id' => 'Zti',
			'zod_price' => 'Zod Price',
			'zod_type' => 'Zod Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zod_id',$this->zod_id);
		$criteria->compare('zo_id',$this->zo_id,true);
		$criteria->compare('zp_id',$this->zp_id);
		$criteria->compare('zti_id',$this->zti_id);
		$criteria->compare('zod_price',$this->zod_price);
		$criteria->compare('zod_type',$this->zod_type);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * @根据订单id返回订单优惠记录.
	 */
	public function getOrderDiscount($orderid)
	{
		$sql = 'select * from {{ord_discount}} where zo_id='.$orderid;
		$discountArray = Yii::app()->db->createCommand($sql)->queryAll();
		return $discountArray;
	}

	/**
	 * @根据订单id返回优惠类型.
	 */
	public static function getDiscountType($typeid=null)
	{
		$types = array(
				0=>'无优惠',
				1=>'团购',
				2=>'限时抢购',
				3=>'折扣',
				4=>'优惠券',
				5=>'优惠套餐',
				);
		return !empty($typeid)? $types[$typeid] : $types[0];
	}

	/**
	 * @根据订单id返回优惠类型.
	 */
	public static function getfavorableType($typeid=1)
	{
		if($typeid==1){
			$types = array(				
				1=>'使用优惠券',
				2=>'使用满就送',				
				);
		}elseif( $typeid==2 ){
			$types = array(				
				1=>'使用优惠券',							
				);
		}else{
			$types = array(				
				2=>'使用满就送',						
				);
		}
		return $types;
	}
}